package
{
	import flash.desktop.NativeApplication;
	import flash.display.Shape;
	import flash.events.AccelerometerEvent;
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.KeyboardEvent;
	import flash.sensors.Accelerometer;
	import flash.system.Capabilities;
	import flash.ui.Keyboard;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	
	/**
	 * ...
	 * @author
	 */
	public class Main extends Sprite
	{
		
		public function Main():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.addEventListener(Event.DEACTIVATE, deactivate);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, checkKeypress);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, stageCheckKeypress);
			
			// touch or gesture?
			Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
			
			trace("debugging");
			
			
			var sh:Shape = new Shape();
			sh.graphics.beginFill(0);
			sh.graphics.drawRect(200, 200, 300, 300);
			sh.graphics.endFill();
			
			addChild(sh);
			
			//listenAccelerometer();
			
		}
		
		private function listenAccelerometer():void 
		{
			var accl:Accelerometer;
			if (Accelerometer.isSupported)
			{
				accl = new Accelerometer();
				accl.addEventListener(AccelerometerEvent.UPDATE, updateHandler);
			}
			else
			{
				trace("Accelerometer feature not supported");
			}
		}
		
		private function onAddedToStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			
			trace("fullscreen", stage.fullScreenWidth, stage.fullScreenHeight);
			trace("capabilities",  Capabilities.screenResolutionX, Capabilities.screenResolutionY);
			trace("current stage", stage.width, stage.height);
			trace("current", width, height);
		}
		
		private function updateHandler(evt:AccelerometerEvent):void
		{
			trace("X: " + evt.accelerationX + 
				  "Y: " + evt.accelerationY + 
				  "Z: " + evt.accelerationZ);
		}
			
		private function stageCheckKeypress(event:KeyboardEvent):void
		{
			switch (event.keyCode)
			{
				case Keyboard.BACK: 
					event.preventDefault();
					trace("stage Back key is pressed.");
					break;
				case Keyboard.HOME: 
					trace("stage Home key is pressed.");
					break;
				case Keyboard.SEARCH: 
					trace("stage Search key is pressed.");
					break;
			}
		}
		
		public function checkKeypress(event:KeyboardEvent):void
		{
			switch (event.keyCode)
			{
				case Keyboard.BACK: 
					event.preventDefault();
					trace("Back key is pressed.");
					break;
				case Keyboard.HOME: 
					trace("Home key is pressed.");
					break;
				case Keyboard.SEARCH: 
					trace("Search key is pressed.");
					break;
			}
		}
		
		private function deactivate(e:Event):void
		{
			// auto-close
			trace("stopping");
			NativeApplication.nativeApplication.exit();
		}
	
	}

}